package com.lingx.utils;

import java.io.File;
import java.io.FileFilter;
import java.nio.charset.Charset;

import org.apache.commons.io.FileUtils;

import com.alibaba.fastjson.JSON;

public class FileUtilsMain {

	public static void main(String[] args) {
		String cmd="";
		System.out.println("=================FileUtilsMain=======================");
		System.out.println(JSON.toJSONString(args));
		if(args.length>0)cmd=args[0];
		switch(cmd) {
		case "replaceappjs":
			replaceFile(args);
			break;
			
		}
	}
	
	/**
	 * exe cmd filepath replace1 replace2 
	 */
	public static void replaceFile(String args[]) {
		try {
			File dir=new File(args[1]);
			File files[]=dir.listFiles(new FileFilter() {
				@Override
				public boolean accept(File pathname) {
					return pathname.getName().startsWith("app")&&pathname.getName().endsWith("js");
				}});
			System.out.println("符合条件的文件数:"+files.length);
			for(File file:files) {
				System.out.println("正在处理:"+file.getName());
				String temp=FileUtils.readFileToString(file, Charset.forName("UTF-8"));
				temp=temp.replace(args[2], args[3]);
				FileUtils.write(file, temp, Charset.forName("UTF-8"));
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
